package com.ruoyi.web.group.skill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.group.skill.entity.SkillAssessment;
import com.ruoyi.web.group.skill.service.SkillAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/assessments")
public class SkillAssessmentController {

    @Autowired
    private SkillAssessmentService skillAssessmentService;

    // 创建评估记录
    @PostMapping
    public AjaxResult create(@RequestBody SkillAssessment skillAssessment) {
        boolean saved = skillAssessmentService.save(skillAssessment);
        return saved ? AjaxResult.success(skillAssessment) : AjaxResult.error("创建失败");
    }

    // 更新评估记录
    @PostMapping ("/{id}")
    public AjaxResult update(@PathVariable Integer id, @RequestBody SkillAssessment assessment) {
        assessment.setAssessmentId(id);
        boolean updated = skillAssessmentService.updateById(assessment);
        return updated ? AjaxResult.success(assessment) : AjaxResult.error("更新失败");
    }

    // 删除评估记录
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        boolean removed = skillAssessmentService.removeById(id);
        return removed ? AjaxResult.success(null) : AjaxResult.error("删除失败");
    }

    // 获取单个评估详情
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Integer id) {
        SkillAssessment assessment = skillAssessmentService.getById(id);
        return assessment != null ? AjaxResult.success(assessment) : AjaxResult.error("未找到该记录");
    }

    // 分页查询评估记录
    @GetMapping
    public AjaxResult list(@RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<SkillAssessment> page = skillAssessmentService.page(new Page<SkillAssessment>(pageNum, pageSize));
        return AjaxResult.success(page);
    }

    // 根据球员ID查询评估记录
    @GetMapping("/player/{playerId}")
    public AjaxResult listByPlayerId(@PathVariable Integer playerId) {
        List<SkillAssessment> assessments = skillAssessmentService.listByPlayerId(playerId);
        return AjaxResult.success(assessments);
    }

    // 根据技能类型查询评估记录
    @GetMapping("/skill-type/{skillType}")
    public AjaxResult listBySkillType(@PathVariable Integer skillType) {
        List<SkillAssessment> assessments = skillAssessmentService.listBySkillType(skillType);
        return AjaxResult.success(assessments);
    }
}